unit RelClientes;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, Buttons, DB, DBTables,DateUtils;

type
  TFormRelClientes = class(TForm)
    RadioGroupRelatorio: TRadioGroup;
    RadioGroupOrdem: TRadioGroup;
    GroupBoxData: TGroupBox;
    Label2: TLabel;
    DateTimePicker2: TDateTimePicker;
    DateTimePicker1: TDateTimePicker;
    Label1: TLabel;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    QueryRelCliente: TQuery;
    GroupBoxCliente: TGroupBox;
    Label4: TLabel;
    StaticTextCliente: TStaticText;
    QueryCliente: TQuery;
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure RadioGroupRelatorioClick(Sender: TObject);
    procedure StaticTextClienteClick(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
    procedure ImprimeClientes;
    procedure ImprimePedidos;
    procedure ImprimeABC;
  public
    { Public declarations }
  end;

var
  FormRelClientes: TFormRelClientes;
  sql,vordem:String;

implementation

uses QRelClientes, QRelPedClientes, Principal, ProcuraCliente,
  QRelPedClientesABC;

{$R *.dfm}

procedure TFormRelClientes.BitBtn1Click(Sender: TObject);
begin

if RadioGroupRelatorio.ItemIndex = 0 then
   ImprimeClientes;


if RadioGroupRelatorio.ItemIndex = 1 then
   ImprimePedidos;

if RadioGroupRelatorio.ItemIndex = 2 then
   ImprimeABC;

end;
procedure TFormRelClientes.ImprimeClientes;
begin
if assigned(FormQRelClientes) then
     FormQRelClientes := nil;

     FormQRelClientes := TFormQRelClientes.Create(Self);

   with FormQRelClientes do
   begin

   QRTitulo.Caption := FormPrincipal.vEmpresa;
   QRTitulo2.Caption := FormPrincipal.vEmpresa2;
   QREndereco.Caption := FormPrincipal.vEndereco;

    //FormQRelClientes := TFormQRelClientes.Create(Self);

    if FormRelClientes.RadioGroupOrdem.ItemIndex = 0 then
       vOrdem := 'nm'
    else
       vOrdem := 'telefone1';

    sql := 'select cliente_tel(id) as telefone,st_cliente_name(id_st_cliente) as st_cliente,cliente.* from cliente order by ' + vOrdem;

    QueryRelCliente.SQL.Clear;
    QueryRelCliente.SQL.Add(sql);
    QueryRelCliente.Close;
    QueryRelCliente.Open;

    QRLabelTotal.Caption := 'Total : ' + Trim(IntToStr(QueryRelCliente.RecordCount));


    //FormQRelClientes.Close;
   end;

   FormQRelClientes.QuickRep1.PreviewModal;
end;

procedure TFormRelClientes.ImprimePedidos;
begin

if StaticTextCliente.Caption = 'Clique para escolher cliente' then
   abort;

if assigned(FormQrelPedClientes) then
     FormQRelPedClientes := nil;
  FormQRelPedClientes := TFormQRelPedClientes.Create(Self);

   with FormQRelPedClientes do
   begin

      QRTitulo.Caption := FormPrincipal.vEmpresa;
      QRTitulo2.Caption := FormPrincipal.vEmpresa2;
      QREndereco.Caption := FormPrincipal.vEndereco;
      
      QueryPedido.ParamByName('p_cliente').AsInteger := FormPrincipal.vProcIDCliente;
      QueryPedido.ParamByName('p_data1').AsString := FormatDateTime('yyyy-mm-dd',incDay(DateTimePicker1.Date,-1));
      QueryPedido.ParamByName('p_data2').AsString := FormatDateTime('yyyy-mm-dd',incDay(DateTimePicker2.Date,1));

      QueryPedido.Close;
      QueryPedido.Open;

      QuickRep1.PreviewModal;

   end;
  
end;

procedure TFormRelClientes.ImprimeABC;
var  xAcumulado: Currency;
     xPct: String;
begin

    //if StaticTextCliente.Caption = 'Clique para escolher cliente' then
    //   abort;

    if assigned(FormQRelClientesABC) then
         FormQRelClientesABC := nil;
         
    FormQRelClientesABC := TFormQRelClientesABC.Create(Self);

   xAcumulado := 0;
   xPct       := '';

   with FormQRelClientesABC do
   begin

      QRTitulo.Caption := FormPrincipal.vEmpresa;
      QRTitulo2.Caption := FormPrincipal.vEmpresa2;
      QREndereco.Caption := FormPrincipal.vEndereco;
      
      //QueryPedido.ParamByName('p_cliente').AsInteger := FormPrincipal.vProcIDCliente;
      //QueryPedido.ParamByName('p_data1').AsString := FormatDateTime('yyyy-mm-dd',incDay(DateTimePicker1.Date,-1));
      //QueryPedido.ParamByName('p_data2').AsString := FormatDateTime('yyyy-mm-dd',incDay(DateTimePicker2.Date,1));

      //QueryPedido.Close;
      //QueryPedido.Open;

      CdsClientes.CreateDataSet;
      CdsClientes.DisableControls ;
      CdsClientes.EmptyDataset;

      QuerySoma.open;
      QueryClientes.open;

      while not QueryClientes.Eof do
      begin
          CdsClientes.append;
          CdsClientesnome.AsString := QueryClientes.FieldByname('nm').AsString;
          CdsClientestelefone.AsString := QueryClientes.FieldByname('telefone1').AsString;
          CdsClientestotal_consumo.AsCurrency := QueryClientes.FieldByname('total').AsCurrency;
          CdsClientesdt_ultima_compra.AsString := FormPrincipal.FormataData(QueryClientes.FieldByname('dt_ult_compra').AsDateTime,4);

          xAcumulado := xAcumulado + QueryClientes.FieldByname('total').AsCurrency;
          CdsClientesacumulado.AsCurrency := xAcumulado;

          xPct:= formatCurr('###,###,##0.00',(xAcumulado / QuerySoma.FieldByName('soma').AsCurrency) * 100);
          CdsClientesporcentagem.AsFloat := StrToFloat(xPct);

          CdsClientes.Post;
          QueryClientes.Next;
      end;

      CdsClientes.EnableControls;

      QuickRep1.PreviewModal;

      
      QueryClientes.Close;
      QuerySoma.close;
   end;

end;

procedure TFormRelClientes.BitBtn2Click(Sender: TObject);
begin
self.close;
end;

procedure TFormRelClientes.RadioGroupRelatorioClick(Sender: TObject);
begin
if RadioGroupRelatorio.ItemIndex = 0 then
begin
   RadioGroupOrdem.Items.Clear;
   RadioGroupOrdem.Items.Add('Nome');
   RadioGroupOrdem.Items.Add('Telefone');

   RadioGroupOrdem.ItemIndex := 0;
   end;


RadioGroupOrdem.Visible := (RadioGroupRelatorio.ItemIndex = 0);

GroupBoxData.Visible := (RadioGroupRelatorio.ItemIndex = 1);
GroupBoxCliente.Visible := (RadioGroupRelatorio.ItemIndex = 1);
end;

procedure TFormRelClientes.StaticTextClienteClick(Sender: TObject);
begin
if assigned(FormProcuraCliente) then
     FormProcuraCliente := nil;
     
  FormProcuraCliente := TFormProcuraCliente.Create(Self);
  FormProcuraCliente.ShowModal;

  QueryCliente.ParamByName('p_id').AsInteger := FormPrincipal.vProcIDCliente;
  QueryCliente.Close;
  QueryCliente.Open;

  StaticTextCliente.Caption := QueryCliente.FieldByName('nm').AsString;

end;

procedure TFormRelClientes.FormActivate(Sender: TObject);
begin
FormPrincipal.vProcIDCliente := 0;

DateTimePicker1.Date := now;
DateTimePicker2.Date := now;

end;



end.
